package android.textIt.frontend.adapter;

import java.util.List;

import android.content.Context;
import android.textIt.R;
import android.textIt.backend.model.Message;
import android.textIt.backend.model.User;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListMessageAdapter extends ArrayAdapter<List<Message>> {
	 private Context context;
	 private List<List<Message>> items;
	 
   public ListMessageAdapter(Context context, List<List<Message>> messageList) {
     super(context, R.layout.li_message_list, messageList);
     this.context = context;
     this.items = messageList;
   }
   
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater vi = 
          	 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = vi.inflate(R.layout.li_message_list, null);
       }
       List<Message> list = items.get(position);
       Message msg = list.get(0);
       TextView tvMembers = (TextView) convertView.findViewById(R.id.TxtMessageMembers);
       tvMembers.setText(msg.getSender().getName());
       TextView tvMsgPreview = (TextView) convertView.findViewById(R.id.TxtMessagePreview);
       tvMsgPreview.setText(msg.getContent());
       return convertView;
   }
   
   private String membersToString(List<User> list){
  	 StringBuilder sb = new StringBuilder();
  	 for (User user:list){
  		 sb.append(user.getName() +", ");
  	 }
  	 sb.delete(sb.length()-2, sb.length());
  	 return sb.toString();
   }
}
